#!/bin/bash

echo "========================================"
echo " Spring Boot 3 OAuth2 + JWT SSO 系统"
echo "========================================"
echo

echo "正在编译项目..."
mvn clean install -DskipTests
if [ $? -ne 0 ]; then
    echo "编译失败！"
    exit 1
fi

echo
echo "编译完成！准备启动服务..."
echo

echo "1. 启动 OAuth2 授权服务器 (端口: 9000)..."
cd oauth2-authorization-server
nohup mvn spring-boot:run > ../oauth2-server.log 2>&1 &
OAUTH2_PID=$!
cd ..

echo "等待授权服务器启动完成（约30秒）..."
sleep 30

echo "2. 启动 OA 系统 (端口: 8080)..."
cd oa-system
nohup mvn spring-boot:run > ../oa-system.log 2>&1 &
OA_PID=$!
cd ..

echo "等待 OA 系统启动..."
sleep 15

echo "3. 启动风控系统 (端口: 8081)..."
cd risk-control-system
nohup mvn spring-boot:run > ../risk-system.log 2>&1 &
RISK_PID=$!
cd ..

echo
echo "========================================"
echo " 所有服务启动完成！"
echo "========================================"
echo
echo "进程 ID："
echo " - OAuth2 授权服务器: $OAUTH2_PID"
echo " - OA 系统:          $OA_PID"
echo " - 风控系统:         $RISK_PID"
echo
echo "访问地址："
echo " - OAuth2 授权服务器: http://www.oauth2server.com:9000"
echo " - OA 系统:          http://www.oauth2server.com:8080"
echo " - 风控系统:         http://www.oauth2server.com:8081"
echo
echo "测试账号："
echo " - 管理员:    admin / 123456"
echo " - OA用户:    oa_user / 123456"
echo " - 风控专员:  risk_user / 123456"
echo " - 普通用户:  user / 123456"
echo
echo "日志文件："
echo " - OAuth2 服务器: oauth2-server.log"
echo " - OA 系统:      oa-system.log"
echo " - 风控系统:     risk-system.log"
echo
echo "请等待所有服务完全启动后再进行测试..."
echo "========================================"

# 创建停止脚本
cat > stop-all.sh << 'EOF'
#!/bin/bash
echo "正在停止所有服务..."

# 查找并停止 Spring Boot 应用
pkill -f "spring-boot:run"

echo "所有服务已停止！"
EOF

chmod +x stop-all.sh

echo "提示：使用 './stop-all.sh' 可以停止所有服务" 